package com.hck.app.service;

import com.hck.api.model.*;
import com.hck.domain.user.model.entity.UserEntity;
import com.hck.domain.user.model.valobj.LoginRequest;
import com.hck.domain.user.model.valobj.LoginResult;
import com.hck.domain.user.model.valobj.RegisterRequest;
import com.hck.domain.user.model.valobj.RegisterResult;
import com.hck.domain.user.service.IUserService;
import com.hck.domain.user.service.UserDomainService;
import com.hck.types.model.Response;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements IUserService {
    
    @Resource
    private UserDomainService userDomainService;

    
    @Resource
    private HttpServletRequest request;
    
    @Override
    public UserRegisterResponseDTO register(UserRegisterRequestDTO requestDTO) {
        // 1. 获取客户端IP
        String clientIp = getClientIp();
        
        // 2. 构建领域层的注册请求
        RegisterRequest registerRequest = new RegisterRequest();
        BeanUtils.copyProperties(requestDTO, registerRequest);
        registerRequest.setRegisterIp(clientIp);
        
        // 3. 调用领域服务进行注册
        RegisterResult registerResult = userDomainService.register(registerRequest);
        
        // 4. 转换为API层的响应DTO
        UserRegisterResponseDTO responseDTO = new UserRegisterResponseDTO();
        responseDTO.setSuccess(registerResult.isSuccess());
        responseDTO.setErrorMessage(registerResult.getErrorMessage());
        
        if (registerResult.isSuccess()) {
            responseDTO.setUserId(registerResult.getUserId());
            responseDTO.setUsername(registerResult.getUsername());
            responseDTO.setRealName(registerResult.getRealName());
        }
        
        return responseDTO;
    }
    
    @Override
    public UserLoginResponseDTO login(UserLoginRequestDTO requestDTO) {
        // 1. 获取客户端IP
        String clientIp = getClientIp();
        
        // 2. 构建领域层的登录请求
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setUsername(requestDTO.getUsername());
        loginRequest.setPassword(requestDTO.getPassword());
        loginRequest.setLoginIp(clientIp);
        
        // 3. 调用领域服务进行登录
        LoginResult loginResult = userDomainService.login(loginRequest);
        
        // 4. 转换为API层的响应DTO
        UserLoginResponseDTO responseDTO = new UserLoginResponseDTO();
        responseDTO.setSuccess(loginResult.isSuccess());
        responseDTO.setErrorMessage(loginResult.getErrorMessage());
        
        if (loginResult.isSuccess()) {
            responseDTO.setUserId(loginResult.getUserId());
            responseDTO.setUsername(loginResult.getUsername());
            responseDTO.setRealName(loginResult.getRealName());
            responseDTO.setToken(loginResult.getToken());
            responseDTO.setImageURL(loginResult.getImageURL());
        }
        
        return responseDTO;
    }
    
    @Override
    public UserInfoDTO getUserInfo(Long userId) {
        // 1. 调用领域服务获取用户信息
        UserEntity user = userDomainService.getUserById(userId);
        
        if (user == null) {
            return null;
        }
        
        // 2. 转换为API层的响应DTO
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        BeanUtils.copyProperties(user, userInfoDTO);
        userInfoDTO.setUserId(user.getId());
        
        return userInfoDTO;
    }
    
    @Override
    public String queryUserInfo(String req) {
        // 保留原有方法实现
        return "查询用户信息: " + req;
    }

    @Override
    public boolean authentic(AuthenticateRequestDTO authenticateRequestDTO) {
        return userDomainService.authentic(authenticateRequestDTO);
    }

    @Override
    public boolean updatePassword(UpdatePasswordRequestDTO requestDTO) {
        return userDomainService.updatePassword(requestDTO);
    }

    @Override
    public void update(UpdateInfoDTO dto) {
        userDomainService.update(dto);
    }

    /**
     * 获取客户端IP地址
     */
    private String getClientIp() {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
} 